package com.net.Thread;

class Message
{
	private String text;
	
	Message(String text)
	{
		this.text = text;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}
}


class Waiter implements Runnable
{
   Message message;
   
   Waiter(Message message)
   {
	   this.message = message;
   }
	
	@Override
	public void run()
	{
		synchronized (message) 
		{
			System.out.println("text message is  " +message.getText());
			System.out.println("waiter going to wait" );	
			try
			{
				message.wait(2000);
			} 
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
			
			
		}
		
		System.out.println(" waiter called" +message.getText());
	}
	
}

class Producer implements Runnable
{
   Message message;
   
   Producer(Message message)
   {
	   this.message = message;
   }
	
	@Override
	public void run() 
	{
		System.out.println("producer going to sleep");
		try
		{
			Thread.sleep(300);
		} catch (InterruptedException e) 
		{
			e.printStackTrace();
		}
		
		synchronized (message)
		{
			System.out.println("notify the waiter");
			message.setText(" notify the waiter");
			message.notify();
		}
		
		
	}
	
}


public class ProduceConsumer {

	public static void main(String[] args)
	{
		Message message = new Message("hello");
		Waiter waiter = new Waiter(message);
		
		Thread thread = new Thread(waiter, "waiterThread");
		thread.start();
		
		
		Producer producer = new Producer(message);
		
		Thread t2 = new Thread(producer,"producerThread");
		t2.start();
		

	}

}
